package com.owen.java.demo.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.owen.java.demo.excel.entity.Data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CreateExcel {

    public static void main(String[] args) throws IOException {
        File file=new File("G:\\工作\\大数据\\铜川数据\\铜川市1.csv");

        List<Data> xingQu=new ArrayList<>();
        List<Data> yiJunXian=new ArrayList<>();
        List<Data> yaoZhouQu=new ArrayList<>();
        List<Data> yinTaiQu=new ArrayList<>();
        List<Data> wangYiQu=new ArrayList<>();

        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        String data=bufferedReader.readLine();
        while (Objects.nonNull(data=bufferedReader.readLine()) && !"".equals(data)){
            if(data.startsWith("新区")){
                xingQu.add(createData(data));
            }else if(data.startsWith("耀州")){
                yaoZhouQu.add(createData(data));
            }else if(data.startsWith("宜君")){
                yiJunXian.add(createData(data));
            }else if(data.startsWith("印台")){
                yinTaiQu.add(createData(data));
            }else if(data.startsWith("王益")){
                wangYiQu.add(createData(data));
            }
        }

        String fileName = "G:\\工作\\大数据\\铜川数据\\data.xlsx";
        // 这里 需要指定写用哪个class去读，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        ExcelWriter excelWriter = EasyExcel.write(fileName, Data.class).build();
        WriteSheet writeSheet0 = EasyExcel.writerSheet(0,"新区").build();
        excelWriter.write(xingQu, writeSheet0);
        WriteSheet writeSheet1 = EasyExcel.writerSheet(1,"耀州").build();
        excelWriter.write(yaoZhouQu, writeSheet1);
        WriteSheet writeSheet2 = EasyExcel.writerSheet(2,"宜君").build();
        excelWriter.write(yiJunXian, writeSheet2);
        WriteSheet writeSheet3 = EasyExcel.writerSheet(3,"印台").build();
        excelWriter.write(yinTaiQu, writeSheet3);
        WriteSheet writeSheet4 = EasyExcel.writerSheet(4,"王益").build();
        excelWriter.write(wangYiQu, writeSheet4);

        excelWriter.finish();
    }

    private static Data createData(String data) {
        Data result=new Data();
        String[] split = data.split(",");
        result.setName(split[0]);
        result.setBscCode(split[1]);
        result.setCiCode(split[2]);
        result.setLng(split[3]);
        result.setLat(split[4]);
        return result;
    }

}
